package com.black.web.app.my.myorders;

import com.black.app.domain.req.my.myorders.MyOrderAddReq;
import com.black.app.domain.req.my.myorders.MyOrdersEditReq;
import com.black.app.domain.resp.my.myorders.UserOrderDetails;
import com.black.app.domain.resp.my.myorders.UserOrdersResp;
import com.black.app.service.store.AppStoreOrderDetailsService;
import com.black.app.service.store.AppStoreOrdersService;
import com.black.common.core.controller.BaseController;
import com.black.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@Api(tags = "app-我的订单相关Api")
@RestController
@RequestMapping("/app/my/orders")
public class MyOrdersController extends BaseController {

    @Autowired
    private AppStoreOrdersService myOrdersService;
    @Autowired
    private AppStoreOrderDetailsService myOrderDetailsService;

    /**
     * 获取用户订单列表
     * @param userId
     * @return
     */
    @ApiOperation(value = "获取用户订单列表")
    @GetMapping(value = "/userOrders/{user_id}")
    public R<PageInfo<UserOrdersResp>> findByUserId(@PathVariable(value = "user_id") Long userId){
        PageInfo<UserOrdersResp> page = this.myOrdersService.userOrderList(userId);
        return R.ok(page);
    }

    /**
     * 根据订单编号查询
     * 订单详情
     * @param orderCode
     * @return
     */
    @ApiOperation(value = "获取订单详情")
    @GetMapping(value = "/details/{order_code}")
    public R<UserOrderDetails> findByCode(@PathVariable(value = "order_code") String orderCode){
        UserOrderDetails orderMsg = this.myOrdersService.findByCode(orderCode);
        return  R.ok(orderMsg);
    }

    /**
     * 新增订单
     * @param req
     * @return
     */
    @ApiOperation(value = "新增订单")
    @PostMapping
    public R add(@RequestBody @Valid MyOrderAddReq req){
        boolean res = this.myOrdersService.add(req);
        boolean resDetails = this.myOrderDetailsService.addOrderDetails(req);
        return super.isSuccess(res&&resDetails);
    }

    /**
     * 修改订单
     * @param req
     * @return
     */
    @ApiOperation(value = "修改订单")
    @PutMapping
    public R edit(@RequestBody @Valid MyOrdersEditReq req){
        boolean res = this.myOrdersService.edit(req);
        return super.isSuccess(res);
    }
    /**
     * 删除订单
     * @param orderCodes
     * @return
     */
   // @PreAuthorize("@ss.hasAppPermi()")
    @ApiOperation(value = "删除订单")
    @DeleteMapping(value = "/{order_codes}")
    public R delete(@PathVariable(value = "order_codes") String[] orderCodes){
        boolean res = this.myOrdersService.deleteByCode(orderCodes);
        boolean resDetails = this.myOrderDetailsService.deleteByCode(orderCodes);
        return super.isSuccess(res && resDetails);
    }



}
